<?php
namespace App\Http\Controllers\Api\Buz;

use App\Http\Controllers\Controller;
use App\Lib\Code;
use App\Repositories\Criteria\AuthUserCriteria;
use App\Repositories\Eloquent\RechargeOrderRepository;
use App\Services\ApiResponseService;
use App\Services\RechargeOrderNotifyService;
use Illuminate\Http\Request;

class RechargeOrderController extends Controller
{
    protected $orderRepository;

    public function __construct(RechargeOrderRepository $rechargeOrderRepository)
    {
        $this->orderRepository = $rechargeOrderRepository;
        $this->orderRepository->pushCriteria(AuthUserCriteria::class);
    }

    public function show(Request $request, $id)
    {
        $order = $this->orderRepository->find($id, ['plat_no', 'merchant_no', 'order_amt', 'order_status', 'order_data', 'req_ip', 'created_at', 'updated_at',]);
        return ApiResponseService::returnData($order);
    }

    public function callback(RechargeOrderNotifyService $rechargeOrderNotifyService, $id)
    {
        $order = $this->orderRepository->findWhere(['order_status' => 1, 'id' => $id])->first();
        if ( ! $order) {
            return ApiResponseService::showError(Code::NOTFOUND);
        }
        $res = $rechargeOrderNotifyService->curlRequest($order->notify, 'post', true);
        return ApiResponseService::returnData($res);
    }

}
